<template>
  <div id="app">
    <van-nav-bar
      title="账号登入"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
        { required: true, message: '请填写用户名' },
        { pattern: /^\w{5,}$/, message: '用户名至少包含5个字符' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
        { required: true, message: '请填写密码' },
        { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' },
        ]"
      />
      <div style="margin: 16px">
        <van-button color="rgb(33,185,122)" block type="info" native-type="submit"
          >登入</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="/register">还没有账号去注册</router-link>
  </div>
</template>
<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
// import { Toast } from 'vant'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      // 解构data 获取data里面的token
      const res = await login(values)
      console.log(res)
      console.log(res.body.token)
      setToken(res.body.token) /// token保存在本地存储
      // this.$toast.success('登入成功') // 登入成功弹出提示
      this.$router.push('/my')// 登入跳转到默认路径，首页 */
    },
    onClickLeft () {
      this.$router.back()
    }
  }
}

</script>

<style lang="less">
.van-nav-bar__content{
  background-color:rgb(33,185,122);
}

.van-nav-bar .van-icon{
  color:#fff
}
.van-nav-bar__title{
  color: #fff;
  font-size: 18px;
}
.link{
  margin-left: 33%;
  font-family: "Microsoft YaHei";
  color: #333;
  font-size: 14px;
}
.van-button--info{
  font-size: 14px;
}
</style>
